package com.bonc.web.user;

import com.bonc.pojo.SysUser;
import com.bonc.util.exception.BusinessException;
import com.bonc.util.response.CommResponseEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description: 密码和校验
 * @Author: ChenZhiXiang
 * @CreateDate: 2019/3/1 0001 16:15
 * @Version: 1.0
 */
@Component
public class SysPasswordService {

   @Autowired
    private UserCache userCache;

    public void validate(SysUser sysUser,String password) throws Exception {

        String userName = sysUser.getLoginName();
        // 是否被锁定
        boolean isLock = userCache.isLock(userName);
        // 如果在缓存已存在，证明已被锁定
        if (isLock){
            throw new BusinessException(CommResponseEnum.USER7);
        }
        String msg = "";
        // 如果密码不正确，更新缓存中的数字
        if (!password.equals(sysUser.getPassword())){
            userCache.upCount(userName);
            msg = "密码错误,您还有"+(UserConstants.USER_PASSWORD_ERROR-userCache.getCount(userName)+"机会");
            //将原有的文本替换
            CommResponseEnum.USER8.setMsg(msg);
            throw new BusinessException(CommResponseEnum.USER8);
        }
    }
}

